/**
 * 
 */
package com.icbc.common.fileupload;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

import com.icbc.cte.base.CTEConstance;
import com.icbc.cte.base.TranFailException;
import com.icbc.ctp.core.IContext;
import com.icbc.ctp.core.annotations.Input;
import com.icbc.ctp.core.annotations.Output;
import com.icbc.ctp.core.impl.OperationStep;



/**
 * TODO 压缩文件
 * <pre>
 *
 * </pre>
 *
 * <pre>
 * modify by lenovo on 2016-7-20
 *    fix->1.
 *         2.
 * </pre> 
 */
@Input(name = { "srcFileNumber" , "srcFilePathField", "srcFileNameField" ,"zipFileNameField", "bytes", "encoding" }, 
	   type = {"java:java.lang.String", "java:java.util.List", "java:java.util.List" ,"java:java.lang.String",
		"java:java.lang.String", "java:java.lang.String" }, 
		mustInput = { true,true, true, true, true, true }, 
		isVariable = { true,true, true, true, true, true }, 
		isArray = {false,false,false, false, false, false }, 
		description = { "被压缩文件个数","被压缩文件路径(含存储文件的名称)","被压缩文件名称(显示的名称)", "压缩后文件名称","bytes×1024", "encoding" })
@Output(name = { "retCode", "retMsg","outFileNameField","outFileName"  }, 
		type = { "java:java.lang.String","java:java.lang.String","java:java.lang.String" ,"java:java.lang.String"  }, 
		isArray = { false, false , false, false }, 
		description = {"返回码", "返回信息","压缩文件路径(含名字)" ,"压缩文件名字"})
public class ZipMoreFileOpStep extends OperationStep {
	
	 protected int execute(IContext context)
	    throws TranFailException
	  {
		String [] filePathField = new String [1];
		filePathField [0] = null;
		String [] fileNameField = null;
	    String zipFileNameField = null;
	    String outFileName = null ;
	    String encoding = null;
	    String bytes = null;
	    
	    
	    try {
	    	Integer srcFileNumber = Integer.valueOf((String)context.getValueAt("srcFileNumber"));
	    	if (srcFileNumber>1){
	    		filePathField = (String [])(context.getValueAt("srcFilePathField"));//多个文件
	    		fileNameField = (String [])(context.getValueAt("srcFileNameField"));//多个文件
	    	}else{//单个文件
	    		filePathField = new String [1];
	    		filePathField [0] = String.valueOf(context.getValueAt("srcFilePathField"));
	    		fileNameField = new String [1];
		    	fileNameField [0] = String.valueOf(context.getValueAt("srcFileNameField"));
	    	}
	    } catch (Exception e) {
		    context.setValueAt("retCode", "-1");
		    context.setValueAt("retMsg", "压缩文件异常，异常原因："+e.toString());
		    return 0;
	    }
	    
	    try
	    {
	      zipFileNameField = (String)context.getValueAt( "zipFileNameField");
	    }
	    catch (Exception e) {
	      zipFileNameField = "";
	      context.setValueAt("retCode", "-1");
	      context.setValueAt("retMsg", "压缩文件异常，异常原因："+e.toString());
	      return 0;
	    }
	    
	    try {
	      bytes = (String)context.getValueAt("bytes");
	      if (("".equals(bytes)) || (bytes == null)) {
	        bytes = "10";
	      }
	    } catch (Exception e) {
	      bytes = "10";
	    }
	    
	    try {
	    	encoding = (String)context.getValueAt("encoding");
	    } catch (Exception e) {
	    	encoding = "GBK";
	    }
	    
	    if ("".equals(zipFileNameField.trim()))
	    {
	      zipFileNameField= CTEConstance.getEnvProperty("file_save_path");
	    } 
	    String curdate = DateUtil.toStr(new Date(),"yyyyMMdd HHmmss");
	    outFileName = curdate+".zip";
	    zipFileNameField =zipFileNameField +"/"+outFileName;
	    
	    try {
	    	OutputStream os = new BufferedOutputStream( new FileOutputStream(zipFileNameField) );
	        ZipOutputStream zos = new ZipOutputStream( os );
	        byte[] buf = new byte[Integer.valueOf(bytes)*1024];
	        int len;
	        for (int i=0;i<filePathField.length;i++) {  
	            File file = new File( filePathField[i] );
	            if ( !file.isFile() ) continue;
	            ZipEntry ze = new ZipEntry(fileNameField[i]);
	            zos.putNextEntry( ze );
	            BufferedInputStream bis = new BufferedInputStream( new FileInputStream( file ) );
	            while ( ( len = bis.read( buf ) ) > 0 ) {
	                zos.write( buf, 0, len );
	            }
	            zos.closeEntry();
	        }
	        zos.setEncoding(encoding);//zos.setComment("中文测试");
	        zos.closeEntry();
	        zos.close();
	        
	    } catch (Exception e) {
	    	context.setValueAt("retCode", "-1");
	        context.setValueAt("retMsg", "压缩文件异常，异常原因："+e.toString());
	        context.setValueAt("outFileNameField", "");
	        context.setValueAt("outFileName", "");
	    }
	    context.setValueAt("retCode", "0");
	    context.setValueAt("retMsg", "");
	    context.setValueAt("outFileNameField", zipFileNameField);
	    context.setValueAt("outFileName", outFileName);
	    return 0;
	  }

}
